﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.dal;
namespace SQLEditor.admin.service
{
    public class SqlAnswerService : BaseService
    {
        public string getall()
        {
            var list = from b in DB.sqlAnswer                       
                       
                       select new SqlAnswer
                       {
                           id = b.id,
                           questionId = b.questionId,
                           questionTitle = b.sqlQuestion.title,
                           answer = b.answer,
                           point = b.score,
                           status = b.status,
                           statusStr = b.statusStr,
                           submittime = b.submittime,
                           userId = b.userId,
                           username = b.tbUser.userName+"("+b.tbUser.realName+")"                            
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where) {
            int totalCount = 0;            
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.sqlAnswer.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("sqlAnswer", "*", order, where, pageSize, pageIndex, paramTotalCount)).ToList();
            var list = from b in pager                       
                       select new SqlAnswer
                       {
                           id = b.id,
                           questionId = b.questionId,
                           questionTitle = b.sqlQuestion.title,
                           answer = b.answer,
                           point = b.score,
                           status = b.status,
                           statusStr = b.statusStr,
                           submittime = b.submittime,
                           userId = b.userId,
                           username = b.tbUser.userName + "(" + b.tbUser.realName + ")"
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string add(sqlAnswer b)
        {
            DB.sqlAnswer.Add(b);
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success=true });
            return json;
        }

        public string edit(sqlAnswer b)
        {            
            return "";
        }

        public string delete(string strIds) {
            if (string.IsNullOrEmpty(strIds)) {
                return JsonUtil.ToJson(new { success=false,msg = "没有选择要删除的记录！"});
            }
            string[] arr = strIds.Split(',');
            List<int> ids= new List<int>();
            foreach (var id in arr) {
                ids.Add(int.Parse(id));
            }
            var answers = DB.sqlAnswer.Where(m => ids.Contains(m.id)).ToList();
            if (answers != null)
            {
                answers.ForEach(m => DB.sqlAnswer.Remove(m));
                DB.SaveChanges();
                return JsonUtil.ToJson(new { success = true });
            }
            else {
                return JsonUtil.ToJson(new { success = false,msg="删除失败！" });
            }
        }
        
    }
}